package com.yiboshi.rehrs.util;

import com.alibaba.fastjson.JSONObject;
import com.yiboshi.rehrs.config.RehrsSystemConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.MDC;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.lang.reflect.Member;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author jack 2021/9/9
 */
@Slf4j
public class DingdingUtils {
    static String sendDingdingApi  = "https://oapi.dingtalk.com/robot/send?access_token=8e23e9010cd6a6d5ab334ee434ba48de35fc74c9839ff83e35a8be8b38b14b6b";
    static String secret = "SECae4b6a82816d7ccb866354dfae62083756a3ec64d279856bc188b085efb057c9";
    static Map<String,String> headerMap = new HashMap<>();
    static{
        //headerMap.put(VarConstant.HEADER_TOKEN_KEY, SessionContext.adminToken);
    }

    public static void main(String[] args) {
        String message = "这是一个测试";
        String mobile = "13328052311";
        sendMessageByLocal(message,mobile);
    }

    public static String sign(Long timestamp,String secret){
        //String secret = "this is secret";
        try{
            String stringToSign = timestamp + "\n" + secret;
            Mac mac = Mac.getInstance("HmacSHA256");
            mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
            byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
            String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)),"UTF-8");
            return sign;
        }catch (Exception e){
            log.info("",e);
            return null;
        }


    }

    public static void sendMessageByLocal(String message, String atMobile){
        //message = message;
        String finalMessage = message;
        ThreadPoolUtils.submitTask(()->{
            String key = "sendMessageByLocal"+ finalMessage +atMobile;
            key = MD5.encode(key);
            Boolean byCache10Min = CacheUtils.getByCache10Min(key, Boolean.class);
            if(Objects.equals(byCache10Min,true)){
                return;
            }
            JSONBuilder isAtAll = JSONBuilder.start("isAtAll", false);
            if(StrUtils.isNotBlank(atMobile)){
                isAtAll.put("atMobiles",new String[]{atMobile});
            }
            JSONBuilder atBuilder = JSONBuilder
                    .start("at", isAtAll.end());
            JSONObject params = atBuilder
                    .put("text", JSONBuilder.start("content",
                            finalMessage).end())
                    .put("msgtype", "text").end();
            log.info("params:{}",params);
            long currentTimeMillis = System.currentTimeMillis();
            String sign = sign(currentTimeMillis, secret);
            JSONObject jsonObject = JsoupUtils.postJSON(
                    //SEC73d848c818fe9e403516c3cf7d6c9d03e4cd1d359867a700e394c2890ad40a49
                    sendDingdingApi+"&timestamp="+currentTimeMillis+"&sign="+sign
                    , null, params
            );
            log.info("{}",jsonObject);
            CacheUtils.putByCache10Min(key,true);
        });
    }



}
